package com.ocom.oauth.auth.app;

import com.ocom.oauth.auth.CustomAbstractTokenGranter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 *
 *  说明：
 *    验证码
 *    定义验证码具体逻辑
 */
public class AppPwdTokenGranter extends CustomAbstractTokenGranter {

    private static final String SMS_AUTH = "app_auth_pwd";

    private final AppPwdUserDetailService appPwdUserDetailService;

    public AppPwdTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, AppPwdUserDetailService appPwdUserDetailService) {
        super(tokenServices, clientDetailsService, requestFactory, SMS_AUTH);
        this.appPwdUserDetailService = appPwdUserDetailService;
    }

    @Override
    protected UserDetails getUserDetails(Map<String, String> parameters) {
        String username = parameters.get("username");
        String password = parameters.get("password");

        return appPwdUserDetailService.loadByNameAndCode(username,password);
    }
}
